<?php
namespace app\admin\controller;

//用户管理
class User extends Common
{
	//用户列表
    public function user_list()
    {
		$where=array('a.u_id'=>array('gt',0));
		$name=Request()->param('name');
		$mobile=Request()->param('mobile');
		$status=Request()->param('status');
		$start_time=strtotime(Request()->param('start_time'));
		$end_time=strtotime(Request()->param('end_time'));
		$page=Request()->param('page');
		$size=Request()->param('size');
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where['a.u_real_name']=array('like','%'.$name.'%');
		}
		if($mobile!=''){
			$where['a.u_mobile']=array('like','%'.$mobile.'%');
		}
		if(is_numeric($status)){
			if($status==0){
				$where['a.u_certification']=0;
			}else if($status==1){
				$where['a.u_certification']=1;
			}else if($status==2){
				$where['a.u_certification']=2;
			}else if($status==3){
				$where['a.u_certification']=3;
			}
		}else{
			$status = -1;
		}
		$where_time=array(array('gt',0),array('gt',0));
		if($start_time>0){
			$where_time[]=array('egt',$start_time);
			$start_time=date('Y-m-d H:i:s',$start_time);
		}else{
			$start_time='';
		}
		if($end_time>0){
			$where_time[]=array('elt',$end_time);
			$end_time=date('Y-m-d H:i:s',$end_time);
		}else{
			$end_time='';			
		}
		$where['a.u_register_time']=$where_time;
		$user=db('user a')
			->field("a.*,b.st_name")
			->join("fr_store_type b","a.u_store_type_id = b.st_id",'left')
			->order('a.u_id desc')
			->where($where)
			->limit("$now,$size")
			->select();
		$count=db('user a')
			->where($where)
			->count();
		$page_str=page($page,$size,$count,count($user));//分页
		$this->view->assign('page_str',$page_str);
		$this->view->assign('user',$user);
		$this->view->assign('count',$count);
		$this->view->assign('name',$name);
		$this->view->assign('mobile',$mobile);
		$this->view->assign('status',$status);
		$this->view->assign('start_time',$start_time);
		$this->view->assign('end_time',$end_time);
		$this->view->assign('page',$page);
		$this->view->assign('size',$size);
        return $this->view->fetch();
    }
	//冻结用户
	function user_del(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('user')
				->where(array('u_id'=>$id))
				->update(array('u_status'=>2));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'冻结成功'),'冻结用户');
			}else{
				return return_json(array('code'=>0,'msg'=>'冻结失败'),'冻结用户');
			}
		}
	}
	//启用用户
	function user_use(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('user')
				->where(array('u_id'=>$id))
				->update(array('u_status'=>1));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'冻结成功'),'启用用户');
			}else{
				return return_json(array('code'=>0,'msg'=>'冻结失败'),'启用用户');
			}
		}
	}
	//用户详情
	function user_details(){
		$id=request()->get('id');
		$user=db('user a')
			->field("a.*,b.st_name")
			->join("fr_store_type b","a.u_store_type_id = b.st_id",'left')
			->where(array('a.u_id'=>$id))
			->find();
		$this->view->assign('user',$user);
        return $this->view->fetch();
	}
	//实名申请
	function apply_list(){
		$where=array('a.u_id'=>array('gt',0));
		$name=Request()->param('name');
		$mobile=Request()->param('mobile');
		$status=Request()->param('status');
		$start_time=strtotime(Request()->param('start_time'));
		$end_time=strtotime(Request()->param('end_time'));
		$page=Request()->param('page');
		$size=Request()->param('size');
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where['a.u_real_name']=array('like','%'.$name.'%');
		}
		if($mobile!=''){
			$where['a.u_mobile']=array('like','%'.$mobile.'%');
		}
		if(is_numeric($status)){
			if($status==0){
				$where['a.u_certification']=0;
			}else if($status==1){
				$where['a.u_certification']=1;
			}else if($status==2){
				$where['a.u_certification']=2;
			}else if($status==3){
				$where['a.u_certification']=3;
			}
		}else{
			$status = 2;
			$where['a.u_certification']=2;
		}
		$where_time=array(array('gt',0),array('gt',0));
		if($start_time>0){
			$where_time[]=array('egt',$start_time);
			$start_time=date('Y-m-d H:i:s',$start_time);
		}else{
			$start_time='';
		}
		if($end_time>0){
			$where_time[]=array('elt',$end_time);
			$end_time=date('Y-m-d H:i:s',$end_time);
		}else{
			$end_time='';			
		}
		$where['a.u_register_time']=$where_time;
		$user=db('user a')
			->field("a.*,b.st_name")
			->join("fr_store_type b","a.u_store_type_id = b.st_id",'left')
			->order('a.u_id desc')
			->where($where)
			->limit("$now,$size")
			->select();
		$count=db('user a')
			->where($where)
			->count();
		$page_str=page($page,$size,$count,count($user));//分页
		$this->view->assign('page_str',$page_str);
		$this->view->assign('user',$user);
		$this->view->assign('count',$count);
		$this->view->assign('name',$name);
		$this->view->assign('mobile',$mobile);
		$this->view->assign('status',$status);
		$this->view->assign('start_time',$start_time);
		$this->view->assign('end_time',$end_time);
		$this->view->assign('page',$page);
		$this->view->assign('size',$size);
        return $this->view->fetch();
	}
	//商铺审核
	function apply(){
		if(request()->isAjax()){
			$data=request()->post();
			$id=$data['id'];
			unset($data['id']);
			$user=db('user a')
				->where(array('u_id'=>$id))
				->find();
			if($user['u_certification']==0){
				return return_json(array('code'=>0,'msg'=>'用户还没有进行认证申请'),'商铺审核');
			}
			if($data['u_certification']!=1){
				$data['u_certification']=3;
			}
			$res=db('user')
				->where(array('u_id'=>$id))
				->update($data);
			if($res!==false){
			    if($data['u_certification']==1){
//			        $region=explode(',',$user['u_store_region']);
//			        $address=array('u_id'=>$user['u_id'],'ua_province'=>$region[0],'ua_city'=>$region[1],'ua_area'=>$region[2],'ua_nearby'=>$region[3],'ua_place'=>$user['u_store_place'],'ua_name'=>$user['u_real_name'],'ua_mobile'=>$user['u_real_mobile'],'ua_default'=>1,'ua_status'=>1,'ua_create_time'=>time());
//			        $res1=db("user_address")
//                        ->insertGetId($address);
//			        $res2=db('user_address')
//                        ->where(array('u_id'=>$user['u_id'],'ua_id'=>array('neq',$res1),'ua_status'=>1))
//                        ->update(array('ua_default'=>0));
                }
				return return_json(array('code'=>1,'msg'=>'成功'),'商铺审核');
			}else{
				return return_json(array('code'=>0,'msg'=>'失败'),'商铺审核');
			}
		}else{
			$id=request()->get('id');
			$user=db('user a')
				->field("a.*,b.st_name")
				->join("fr_store_type b","a.u_store_type_id = b.st_id",'left')
				->where(array('a.u_id'=>$id))
				->find();
			$this->view->assign('user',$user);
			return $this->view->fetch();
		}
	}
	//商铺审核失败
	function apply_del(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$user=db('user a')
				->where(array('u_id'=>$id))
				->find();
			if($user['u_certification']==0 || $user['u_certification']==3){
				return return_json(array('code'=>0,'msg'=>'未在申请中或已通过，无法操作'),'商铺审核失败');
			}
			$res=db('user')
				->where(array('u_id'=>$id))
				->update(array('u_certification'=>3,'u_certification_time'=>time()));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'成功'),'商铺审核失败');
			}else{
				return return_json(array('code'=>0,'msg'=>'失败'),'商铺审核失败');
			}
		}
	}
	//商铺类型
	function store_type_list(){
		$where=array('a.st_id'=>array('gt',0));
		$name=Request()->param('name');
		$status=Request()->param('status');
		$start_time=strtotime(Request()->param('start_time'));
		echo $start_time;
		$end_time=strtotime(Request()->param('end_time'));
		$page=Request()->param('page');
		$size=Request()->param('size');
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where['a.st_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.st_status']=1;
			}else{
				$where['a.st_status']=array('neq',1);
			}
		}else{
			$status = 0;
		}
		$where_time=array(array('gt',0),array('gt',0));
		if($start_time>0){
			$where_time[]=array('egt',$start_time);
			$start_time=date('Y-m-d H:i:s',$start_time);
		}else{
			$start_time='';
		}
		if($end_time>0){
			$where_time[]=array('elt',$end_time);
			$end_time=date('Y-m-d H:i:s',$end_time);
		}else{
			$end_time='';			
		}
		$where['a.st_create_time']=$where_time;
		$store_type=db('store_type a')
			->field("a.*,count(b.u_id) as st_number")
			->join("fr_user b","a.st_id = b.u_store_type_id",'left')
			->order('a.st_status,a.st_sort desc')
			->where($where)
			->limit("$now,$size")
			->group("a.st_id")
			->select();
		$count=db('store_type a')
			->where($where)
			->count();
		$page_str=page($page,$size,$count,count($store_type));//分页
		$this->view->assign('page_str',$page_str);
		$this->view->assign('store_type',$store_type);
		$this->view->assign('count',$count);
		$this->view->assign('name',$name);
		$this->view->assign('status',$status);
		$this->view->assign('start_time',$start_time);
		$this->view->assign('end_time',$end_time);
		$this->view->assign('page',$page);
		$this->view->assign('size',$size);
        return $this->view->fetch();
	}
	//添加商铺类型
	function add_store_type(){
		if(request()->isAjax()){
			$data=request()->post();
			if($data['st_name']==''){
				return return_json(array('code'=>0,'msg'=>'类型名称不能为空'),'添加商铺类型');
			}
			if($data['st_sort']<=0){
				$data['st_sort']=0;
			}
			$store_type=db('store_type')->where(array('st_name'=>$data['st_name']))->find();
			if(!empty($store_type)){
				return return_json(array('code'=>0,'msg'=>'已有相同的类型了'),'添加商铺类型');
			}
			$data['st_create_time']=time();
			$res=db('store_type')->insertGetId($data);
			if($res>0){
				return return_json(array('code'=>1,'msg'=>'类型添加成功'),'添加商铺类型');
			}else{
				return return_json(array('code'=>0,'msg'=>'类型添加失败'),'添加商铺类型');
			}
		}else{
			return $this->view->fetch();
		}
	}
	//修改商铺类型
	function restore_type(){
		if(request()->isAjax()){
			$data=request()->post();
			$id=$data['id'];
			unset($data['id']);
			if($data['st_name']==''){
				return return_json(array('code'=>0,'msg'=>'类型名称不能为空'),'修改商铺类型');
			}
			if($data['st_sort']<=0){
				$data['st_sort']=0;
			}
			$store_type=db('store_type')->where(array('st_name'=>$data['st_name'],'st_id'=>array('neq',$id)))->find();
			if(!empty($store_type)){
				return return_json(array('code'=>0,'msg'=>'已有相同的类型了'),'修改商铺类型');
			}
			$data['st_update_time']=time();
			$res=db('store_type')->where(array('st_id'=>$id))->update($data);
			if($res>0){
				return return_json(array('code'=>1,'msg'=>'类型修改成功'),'修改商铺类型');
			}else{
				return return_json(array('code'=>0,'msg'=>'类型修改失败'),'修改商铺类型');
			}
		}else{
			$id=request()->get('id');
			$store_type=db('store_type')->where(array('st_id'=>$id))->find();
			$this->view->assign('store_type',$store_type);
			return $this->view->fetch();
		}
	}
	//冻结商铺类型
	function store_type_del(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('store_type')
				->where(array('st_id'=>$id))
				->update(array('st_status'=>2,'st_update_time'=>time()));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'冻结成功'),'冻结商铺类型');
			}else{
				return return_json(array('code'=>0,'msg'=>'冻结失败'),'冻结商铺类型');
			}
		}
	}
	//解冻商铺类型
	function store_type_use(){
		if(request()->isAjax()){
			$id=request()->post('id');
			$res=db('store_type')
				->where(array('st_id'=>$id))
				->update(array('st_status'=>1,'st_update_time'=>time()));
			if($res!==false){
				return return_json(array('code'=>1,'msg'=>'解冻成功'),'解冻商铺类型');
			}else{
				return return_json(array('code'=>0,'msg'=>'解冻失败'),'解冻商铺类型');
			}
		}
	}
}
